/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-11-29 17:27:33
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-17 15:13:34
 * @FilePath: /pornhub_app/lib/src/views/mine/favorite/views/like_play_list.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/components/back_to_top/floating_back_to_top_scaffold.dart';
import 'package:pornhub_app/components/keep_alive_wrapper.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/no_more/no_more.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/play/play_list_model.dart';
import 'package:pornhub_app/services/user_service.dart';

class LikePlayList extends StatefulWidget {
  const LikePlayList({
    super.key,
  });

  @override
  State<StatefulWidget> createState() => _LikePlayList();
}

class _LikePlayList extends State<LikePlayList> {
  final _scrollController = ScrollController();
  final _pageSize = 40;
  final PagingController<int, PlayListModel> _pagingController =
      PagingController(firstPageKey: 1);

  Future _initListData(int pageKey) async {
    final userId = Get.find<UserService>().user.userId ?? 0;
    final players = await httpInstance.get<PlayListModel>(
        url: 'playlist/list',
        queryMap: {
          'pageSize': _pageSize,
          'page': pageKey,
          'type': 2,
          "userId": userId,
        },
        complete: PlayListModel.fromJson);
    final isLastPage = players.length < _pageSize;
    if (isLastPage) {
      _pagingController.appendLastPage(players);
    } else {
      final nextPageKey = pageKey + 1;
      _pagingController.appendPage(players, nextPageKey as int?);
    }
  }

  @override
  void initState() {
    _pagingController.addPageRequestListener((pageKey) {
      _initListData(pageKey);
    });
    super.initState();
  }

  @override
  void dispose() {
    _pagingController.dispose();
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return KeepAliveWrapper(
      child: FloatingBackToTopScaffold(
        scrollController: _scrollController,
        body: RefreshIndicator(
          onRefresh: () => Future.sync(
            () => _pagingController.refresh(),
          ),
          child: PagedGridView(
            scrollController: _scrollController,
            physics: const AlwaysScrollableScrollPhysics(),
            showNewPageProgressIndicatorAsGridChild: false,
            showNewPageErrorIndicatorAsGridChild: false,
            showNoMoreItemsIndicatorAsGridChild: false,
            padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 8.w),
            pagingController: _pagingController,
            builderDelegate: PagedChildBuilderDelegate<PlayListModel>(
                noMoreItemsIndicatorBuilder: (_) {
              return const NoMore();
            }, noItemsFoundIndicatorBuilder: (_) {
              return const NoData();
            }, itemBuilder: (context, value, index) {
              return Container();

              // PlayListCell(
              //   model: value,
              // );
            }),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                mainAxisSpacing: 15.w,
                crossAxisSpacing: 14.w,
                crossAxisCount: 3,
                childAspectRatio: 114 / 140),
          ),
        ),
      ),
    );
  }
}
